package com.eastedu.template.thread;

import com.eastedu.thread.DefaultThreadFactory;

import java.util.Collection;
import java.util.List;
import java.util.concurrent.*;

/**
 * The type Thread executor.
 *
 * @author luozhenzhong
 */
public class ThreadExecutor {
    private static final ExecutorService executorService = new ThreadPoolExecutor(0, 20, 5, TimeUnit.SECONDS, new LinkedBlockingQueue<>(), new DefaultThreadFactory("report"));

    /**
     * Execute.
     *
     * @param runnable the runnable
     * @param debug    the debug
     */
    public static void execute(Runnable runnable, boolean debug) {
        if (debug) {
            runnable.run();
            return;
        }
        executorService.execute(runnable);
    }

    /**
     * Invoke all list.
     *
     * @param <T>  the type parameter
     * @param task the task
     * @return the list
     * @throws InterruptedException the interrupted exception
     */
    public static <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> task) throws InterruptedException {
        return executorService.invokeAll(task);
    }

    /**
     * Gets executor service.
     *
     * @return the executor service
     */
    public static ExecutorService getExecutorService() {
        return executorService;
    }
}
